<script setup>
import { useRouter } from 'vue-router'
import { useUserStore } from '@/store/useUserStore'
import { login } from '@/api'
import { local } from '@/utils/storage'

const router = useRouter()
const userStore = useUserStore()

onMounted(() => {
  if (local.get('admin-token')) {
    router.push('/')
  }
})

const form = reactive({
  username: 'admin',
  password: '123456',
})

const submit = event => {
  event.preventDefault()
  login(form).then(({ code, data, msg }) => {
    if (code === 200) {
      userStore.setUser(data.user)
      local.set('admin-token', data.token)
      router.push('/')
    }
  })
}

const register = event => {
  console.log('form :>> ', form)
  return
}
</script>

<template>
  <div class="login-page">
    <div class="login-container">
      <h1>登录</h1>
      <form>
        <div class="form-control">
          <input type="text" id="username" v-model="form.username" required />
          <label for="username">账号/邮箱</label>
        </div>
        <div class="form-control">
          <input type="password" id="password" v-model="form.password" required />
          <label for="username">密码</label>
        </div>
        <button type="button" @click="submit">登录</button>
        <button type="button" @click="register">注册</button>
      </form>
    </div>
  </div>
</template>

<style scoped>
.login-page {
  width: 100vw;
  height: 100vh;
  /* 渐变色 */
  background: linear-gradient(to bottom right, #e66465, #9198e5);
  /* background-color: #f8f8f8; */
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.login-container {
  max-width: 400px;
  width: 368px;
  margin: 0 auto;
  padding: 2rem 3rem;
  background: #f8f8f8;
  text-align: center;
  font-size: 1.2rem;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  box-sizing: border-box;
}

form {
  padding: 1rem 0 1rem;
}

.form-control {
  position: relative;
  margin: 2rem 0;
  text-align: left;
  display: flex;
  flex-direction: column-reverse;
  justify-content: flex-end;
}

label {
  font-size: 1rem;
  color: #656565;
  margin: 0.5rem 0;
  position: absolute;
  left: 8px;
  top: 0;
  transition: left 0.3s, top 0.2s;
  pointer-events: none;
}

input {
  font-size: 1rem;
  padding: 0.5rem;
  border-radius: 5px;
  border: 1px solid #c7c7c7;
  background: #f2f2f2;
  width: 100%;
  box-sizing: border-box;
  outline: none;
}

input:valid + label,
input:hover + label {
  display: inline-block;
  position: absolute;
  left: 0;
  top: -35px;
}
input:hover {
  border: 1px solid #4caf50;
}

button {
  background: #4caf50;
  color: #fff;
  padding: 0.5rem 2rem;
  border-radius: 5px;
  border: none;
  cursor: pointer;
  transition: all 0.3s ease;
}
button:last-child {
  background: #ff9900;
  margin-left: 1rem;
}

button:hover {
  background: #3e8e41;
}
button:last-child:hover {
  background: #d5850b;
}
</style>
